package com.ruicar.afs.cloud.afscase.mq.approvesendinfo.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruicar.afs.cloud.afscase.mq.approvesendinfo.PushDataForPos;
import com.ruicar.afs.cloud.afscase.mq.approvesendinfo.service.ApproveAttachmentService;
import com.ruicar.afs.cloud.bizcommon.rules.entity.*;
import com.ruicar.afs.cloud.bizcommon.rules.mapper.*;
import com.ruicar.afs.cloud.bizcommon.rules.service.AfsRuleInfoService;
import com.ruicar.afs.cloud.bizcommon.rules.service.AfsRuleOperateHisService;
import com.ruicar.afs.cloud.common.mq.rabbit.message.AfsTransEntity;
import com.ruicar.afs.cloud.common.mq.rabbit.message.MqTransCode;
import com.ruicar.afs.cloud.image.entity.ComAttachmentManagement;
import com.ruicar.afs.cloud.image.entity.ComAttachmentRule;
import com.ruicar.afs.cloud.image.mapper.ComAttachmentRuleMapper;
import com.ruicar.afs.cloud.image.service.ComAttachmentRuleService;
import com.ruicar.afs.cloud.image.vo.AttachmentDataSendVo;
import com.ruicar.afs.cloud.image.vo.AttachmentRuleVo;
import lombok.AllArgsConstructor;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author tiankai
 * @version 1.0
 * @description: 数据发送
 * @created 2020/7/15 16:14
 */
@Service
@AllArgsConstructor
@Order(Ordered.LOWEST_PRECEDENCE)
@ConditionalOnProperty(prefix = "com.ruicar.afs.cloud.mq.rabbit", name = "enable")
public class ApproveAttachmentServiceImpl implements ApproveAttachmentService {

    private PushDataForPos pushDataForPos;
    private AfsRuleInfoService ruleInfoService;
    private ComAttachmentRuleMapper ruleMapper;
    private AfsRuleInputMappingMapper mappingMapper;
    private AfsRuleItemMapper ruleItemMapper;
    private AfsRuleOperateHisService ruleOperateHisService;
    private AfsRuleTransExpressMapper ruleTransExpressMapper;
    private AfsRuleExpressMapper ruleExpressMapper;
    private AfsRuleExpressAtomMapper ruleExpressAtomMapper;
    private ComAttachmentRuleService ruleService;

    @Override
    public void submitAttachmentInfo(ComAttachmentManagement management) {
        AfsTransEntity<AttachmentDataSendVo> transEntity = new AfsTransEntity();
        transEntity.setTransCode(MqTransCode.AFS_POS_APPLY_CASE_CTM_NEW_ATTACHMENT);
        AttachmentDataSendVo dataSendVo = new AttachmentDataSendVo();
        dataSendVo.setManagement(management);
        List<ComAttachmentRule> ruleList = ruleService.list(Wrappers.<ComAttachmentRule>query().lambda().eq(ComAttachmentRule::getAttachmentId, management.getId()));
        dataSendVo.setRuleList(ruleList);
        List<AttachmentRuleVo> ruleVoList = new ArrayList();
        for (ComAttachmentRule rule : ruleList) {
            AttachmentRuleVo ruleVo = new AttachmentRuleVo();
            List<AfsRuleInfo> list = ruleInfoService.list(Wrappers.<AfsRuleInfo>query().lambda().eq(AfsRuleInfo::getRuleNo, rule.getId()));
            if(list!=null&&!list.isEmpty()){
                ruleVo.setRuleInfo(list);
                List<List<AfsRuleExpress>> ruleExpress = new ArrayList<>();
                List<List<AfsRuleExpressAtom>> ruleExpressAtom = new ArrayList<>();
                List<List<AfsRuleInputMapping>> ruleInputMapping = new ArrayList<>();
                List<List<AfsRuleItem>> ruleItem = new ArrayList<>();
                List<List<AfsRuleOperateHis>> ruleOperateHis = new ArrayList<>();
                List<List<AfsRuleTransExpress>> ruleTransExpress = new ArrayList<>();
                for(AfsRuleInfo afsRuleInfo : list){
                    List<AfsRuleExpress> ruleExpressList = ruleExpressMapper.selectList(Wrappers.<AfsRuleExpress>query().lambda().eq(AfsRuleExpress::getRuleId, afsRuleInfo.getId()));
                    ruleExpress.add(ruleExpressList);
                    List<AfsRuleExpressAtom> ruleExpressAtomList = ruleExpressAtomMapper.selectList(Wrappers.<AfsRuleExpressAtom>query().lambda().eq(AfsRuleExpressAtom::getRuleId, afsRuleInfo.getId()));
                    ruleExpressAtom.add(ruleExpressAtomList);
                    List<AfsRuleInputMapping> ruleInputMappingList = mappingMapper.selectList(Wrappers.<AfsRuleInputMapping>query().lambda().eq(AfsRuleInputMapping::getRuleId, afsRuleInfo.getId()));
                    ruleInputMapping.add(ruleInputMappingList);
                    List<AfsRuleItem> ruleItemList = ruleItemMapper.selectList(Wrappers.<AfsRuleItem>query().lambda().eq(AfsRuleItem::getRuleId, afsRuleInfo.getId()));
                    ruleItem.add(ruleItemList);
                    List<AfsRuleOperateHis> ruleOperateHisList = ruleOperateHisService.list(Wrappers.<AfsRuleOperateHis>query().lambda().eq(AfsRuleOperateHis::getTargetRuleId, afsRuleInfo.getId()));
                    ruleOperateHis.add(ruleOperateHisList);
                    List<AfsRuleTransExpress> ruleTransExpressList = ruleTransExpressMapper.selectList(Wrappers.<AfsRuleTransExpress>query().lambda().eq(AfsRuleTransExpress::getRuleId, afsRuleInfo.getId()));
                    ruleTransExpress.add(ruleTransExpressList);
                }
                ruleVo.setRuleExpress(ruleExpress);
                ruleVo.setRuleExpressAtom(ruleExpressAtom);
                ruleVo.setRuleInputMapping(ruleInputMapping);
                ruleVo.setRuleItem(ruleItem);
                ruleVo.setRuleOperateHis(ruleOperateHis);
                ruleVo.setRuleTransExpress(ruleTransExpress);
                ruleVoList.add(ruleVo);
            }
        }
        dataSendVo.setRuleVoList(ruleVoList);
        transEntity.setData(dataSendVo);
        pushDataForPos.sendAttachmentInfo(transEntity);
    }
}
